package com.n5x.service.impl;

import com.n5x.mapper.discount.DiscountCashMapper;
import com.n5x.mapper.discount.DiscountGiftMapper;
import com.n5x.model.AbstractModel;
import com.n5x.model.discount.DiscountCash;
import com.n5x.model.discount.DiscountCashExample;
import com.n5x.model.discount.DiscountGift;
import com.n5x.model.discount.DiscountGiftExample;
import com.n5x.service.IDiscountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Created by Administrator on 2017/9/22.
 */
@Service
@Transactional
public class IDiscountServiceImpl implements IDiscountService {

    @Autowired
    private DiscountCashMapper discountCashMapper;

    @Autowired
    private DiscountGiftMapper discountGiftMapper;

    public DiscountCash saveDiscountCash(DiscountCash discountCash) {
        Assert.notNull(discountCash, "discountCash对象不能为空");
        if (StringUtils.isNotBlank(discountCash.getId())) {
            discountCashMapper.updateByPrimaryKeySelective(discountCash);
        } else {
            discountCash.init();
            discountCashMapper.insertSelective(discountCash);
        }
        return discountCash;
    }

    public List<DiscountCash> findDiscountCashList(DiscountCashExample.Criteria criteria) {
        DiscountCashExample discountCashExample = new DiscountCashExample();
        if (criteria == null) {
            criteria = discountCashExample.or();
        } else {
            discountCashExample.or(criteria);
        }
        discountCashExample.setOrderByClause(AbstractModel.ORDER);
        return discountCashMapper.selectByExample(discountCashExample);
    }

    public DiscountCash findDiscountCash(String id) {
        Assert.notNull(id, "ID不能为空");
        return discountCashMapper.selectByPrimaryKey(id);
    }

    public DiscountGift saveDiscountGift(DiscountGift discountGift) {
        Assert.notNull(discountGift, "discountGift对象不能为空");
        if (StringUtils.isNotBlank(discountGift.getId())) {
            discountGiftMapper.updateByPrimaryKeySelective(discountGift);
        } else {
            discountGift.init();
            discountGiftMapper.insertSelective(discountGift);

        }
        return discountGift;
    }

    public List<DiscountGift> findDiscountGiftList(DiscountGiftExample.Criteria criteria) {
        DiscountGiftExample discountGiftExample = new DiscountGiftExample();
        if (criteria == null) {
            criteria = discountGiftExample.or();
        } else {
            discountGiftExample.or(criteria);
        }
        discountGiftExample.setOrderByClause(AbstractModel.ORDER);
        return discountGiftMapper.selectByExampleWithBLOBs(discountGiftExample);
    }

    public DiscountGift findDiscountGift(String id) {
        Assert.notNull(id, "ID不能为空");
        return discountGiftMapper.selectByPrimaryKey(id);
    }
}
